package xyz.heyaoshare.config.minio;

import lombok.AllArgsConstructor;
import lombok.Getter;
import xyz.heyaoshare.utils.StrUtils;

/**
 * Minio 桶策略
 * @author YueHe
 * @version 0.0.1
 * @since 2025/6/30 上午9:29
 */
@Getter
@AllArgsConstructor
public enum BucketPolicyEnum {

    PUBLIC, PRIVATE;


    public static String policyMatch(BucketPolicyEnum policyEnum, String bucketName) {
        return switch (policyEnum) {
            case PUBLIC -> StrUtils.format("""
                      {
                      "Version": "2012-10-17",
                      "Statement": [
                        {
                          "Effect": "Allow",
                          "Principal": {
                            "AWS": ["*"]
                          },
                          "Action": [
                            "s3:ListBucket",
                            "s3:ListBucketMultipartUploads",
                            "s3:GetBucketLocation"
                          ],
                          "Resource": [
                            "arn:aws:s3:::{}"
                          ]
                        },
                        {
                          "Effect": "Allow",
                          "Principal": {
                            "AWS": ["*"]
                          },
                          "Action": [
                            "s3:AbortMultipartUpload",
                            "s3:DeleteObject",
                            "s3:GetObject",
                            "s3:ListMultipartUploadParts",
                            "s3:PutObject"
                          ],
                          "Resource": [
                            "arn:aws:s3:::{}/*"
                          ]
                        }
                      ]
                    }
                    """, bucketName, bucketName);
            case PRIVATE -> StrUtils.format("""
                    {
                      "Version": "2012-10-17",
                      "Statement": [
                        {
                          "Effect": "Allow",
                          "Principal": {
                            "AWS": ["*"]
                          },
                          "Action": [
                            "s3:ListBucket",
                            "s3:ListBucketMultipartUploads",
                            "s3:GetBucketLocation"
                          ],
                          "Resource": [
                            "arn:aws:s3:::{}"
                          ]
                        },
                        {
                          "Effect": "Allow",
                          "Principal": {
                            "AWS": ["*"]
                          },
                          "Action": [
                            "s3:GetObject",
                            "s3:ListMultipartUploadParts",
                            "s3:PutObject",
                            "s3:AbortMultipartUpload",
                            "s3:DeleteObject"
                          ],
                          "Resource": [
                            "arn:aws:s3:::{}/*"
                          ]
                        }
                      ]
                    }
                    """, bucketName, bucketName);
        };
    }

}
